﻿
/*************************************************************************
	           DES四种模式下的加密和解密
 ************************************************************************/
#include <iostream>
#include <fstream>
#include <bitset>
#include <string>
#include <vector>
#include <sstream>
#include <ctime>
using namespace std;

bitset<64> key;                // 64位密钥
bitset<48> subKey[16];         // 存放16轮子密钥

// 初始置换表
int IP[] = { 58, 50, 42, 34, 26, 18, 10, 2,
			60, 52, 44, 36, 28, 20, 12, 4,
			62, 54, 46, 38, 30, 22, 14, 6,
			64, 56, 48, 40, 32, 24, 16, 8,
			57, 49, 41, 33, 25, 17, 9,  1,
			59, 51, 43, 35, 27, 19, 11, 3,
			61, 53, 45, 37, 29, 21, 13, 5,
			63, 55, 47, 39, 31, 23, 15, 7 };

// 结尾置换表
int IP_1[] = { 40, 8, 48, 16, 56, 24, 64, 32,
			  39, 7, 47, 15, 55, 23, 63, 31,
			  38, 6, 46, 14, 54, 22, 62, 30,
			  37, 5, 45, 13, 53, 21, 61, 29,
			  36, 4, 44, 12, 52, 20, 60, 28,
			  35, 3, 43, 11, 51, 19, 59, 27,
			  34, 2, 42, 10, 50, 18, 58, 26,
			  33, 1, 41,  9, 49, 17, 57, 25 };

/*------------------下面是生成密钥所用表-----------------*/

// 密钥置换表，将64位密钥变成56位
int PC_1[] = { 57, 49, 41, 33, 25, 17, 9,
			   1, 58, 50, 42, 34, 26, 18,
			  10,  2, 59, 51, 43, 35, 27,
			  19, 11,  3, 60, 52, 44, 36,
			  63, 55, 47, 39, 31, 23, 15,
			   7, 62, 54, 46, 38, 30, 22,
			  14,  6, 61, 53, 45, 37, 29,
			  21, 13,  5, 28, 20, 12,  4 };

// 压缩置换，将56位密钥压缩成48位子密钥
int PC_2[] = { 14, 17, 11, 24,  1,  5,
			   3, 28, 15,  6, 21, 10,
			  23, 19, 12,  4, 26,  8,
			  16,  7, 27, 20, 13,  2,
			  41, 52, 31, 37, 47, 55,
			  30, 40, 51, 45, 33, 48,
			  44, 49, 39, 56, 34, 53,
			  46, 42, 50, 36, 29, 32 };

// 每轮左移的位数
int shiftBits[] = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };

/*------------------下面是密码函数 f 所用表-----------------*/

// 扩展置换表，将 32位 扩展至 48位
int E[] = { 32,  1,  2,  3,  4,  5,
			4,  5,  6,  7,  8,  9,
			8,  9, 10, 11, 12, 13,
		   12, 13, 14, 15, 16, 17,
		   16, 17, 18, 19, 20, 21,
		   20, 21, 22, 23, 24, 25,
		   24, 25, 26, 27, 28, 29,
		   28, 29, 30, 31, 32,  1 };

// S盒，每个S盒是4x16的置换表，6位 -> 4位
int S_BOX[8][4][16] = {
	{
		{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
		{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
		{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
		{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}
	},
	{
		{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
		{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
		{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
		{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}
	},
	{
		{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
		{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
		{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
		{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}
	},
	{
		{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
		{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
		{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
		{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}
	},
	{
		{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
		{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
		{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
		{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}
	},
	{
		{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
		{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
		{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
		{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}
	},
	{
		{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
		{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
		{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
		{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}
	},
	{
		{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
		{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
		{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
		{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}
	}
};

// P置换，32位 -> 32位
int P[] = { 16,  7, 20, 21,
		   29, 12, 28, 17,
			1, 15, 23, 26,
			5, 18, 31, 10,
			2,  8, 24, 14,
		   32, 27,  3,  9,
		   19, 13, 30,  6,
		   22, 11,  4, 25 };

/**********************************************************************/
/*                                                                    */
/*                            下面是DES算法实现                         */
/*                                                                    */
/**********************************************************************/

/**
 *  密码函数f，接收32位数据和48位子密钥，产生一个32位的输出
 */
bitset<32> f(bitset<32> R, bitset<48> k)
{
	bitset<48> expandR;
	// 第一步：扩展置换，32 -> 48
	for (int i = 0; i < 48; ++i)
		/*expandR[47 - i] = R[32 - E[i]];*/
		expandR[ i] = R[ E[i]-1];
	// 第二步：异或
	expandR = expandR ^ k;
	// 第三步：查找S_BOX置换表
	bitset<32> output;
	int x = 0;
	for (int i = 0; i < 48; i = i + 6)
	{
		int row = expandR[ i] * 2 + expandR[i + 5];
		int col = expandR[i +1] * 8 + expandR[i + 2] * 4 + expandR[ i +3] * 2 + expandR[ i + 4];
		//int row = expandR[47 - i] * 2 + expandR[47 - i - 5];
		//int col = expandR[47 - i - 1] * 8 + expandR[47 - i - 2] * 4 + expandR[47 - i - 3] * 2 + expandR[47 - i - 4];
		int num = S_BOX[i / 6][row][col];
		bitset<4> binary(num);
		//output[31 - x] = binary[3];
		//output[31 - x - 1] = binary[2];
		//output[31 - x - 2] = binary[1];
		//output[31 - x - 3] = binary[0];
		output[ x] = binary[3];
		output[ x + 1] = binary[2];
		output[ x + 2] = binary[1];
		output[ x + 3] = binary[0];
		x += 4;
	}
	// 第四步：P-置换，32 -> 32
	bitset<32> tmp = output;
	for (int i = 0; i < 32; ++i)
		/*output[31 - i] = tmp[32 - P[i]];*/
		output[ i] = tmp[ P[i]-1];
	return output;
}

/**
 *  对56位密钥的前后部分进行左移
 */
bitset<28> leftShift(bitset<28> k, int shift)
{
	bitset<28> tmp = k;
	for (int i = 27; i >= 0; --i)
	{
		if (i - shift < 0)
			/*k[i] = tmp[i - shift + 28];*/
			k[i - shift + 28] = tmp[i];
		else
			/*k[i] = tmp[i - shift];*/
			k[i-shift] = tmp[i];
	}
	return k;
}

/**
 *  生成16个48位的子密钥
 */
void generateKeys()
{
	bitset<56> realKey;
	bitset<28> left;
	bitset<28> right;
	bitset<48> compressKey;
	// 去掉奇偶标记位，将64位密钥变成56位
	for (int i = 0; i < 56; ++i)
		/*realKey[55 - i] = key[64 - PC_1[i]];*/
		realKey[ i] = key[ PC_1[i]-1];
	
	cout << "realKey为：";
	for (int i = 0; i < 56; i++) {
		
			cout << realKey[i];
	}
	cout << endl;

	// 生成子密钥，保存在 subKeys[16] 中
	for (int round = 0; round < 16; ++round)
	{
		// 前28位与后28位
		for (int i = 28; i < 56; ++i)
			right[i - 28] = realKey[i];
		for (int i = 0; i < 28; ++i)
			left[i] = realKey[i];
		


		// 左移
		left = leftShift(left, shiftBits[round]);
		right = leftShift(right, shiftBits[round]);

		cout << "前28位为：" << "第" << round + 1 << "次";
		for (int i = 0; i < 28; i++) {

			cout << left[i];
		}
		cout << endl;

		cout << "后28位为：" << "第" << round + 1 << "次";;
		for (int i = 0; i < 28; i++) {

			cout << right[i];
		}
		cout << endl;
		// 压缩置换，由56位得到48位子密钥
		for (int i = 28; i < 56; ++i)
			realKey[i] = right[i - 28];
		for (int i = 0; i < 28; ++i)
			realKey[i] = left[i];
		for (int i = 0; i < 48; ++i)
			//compressKey[47 - i] = realKey[56 - PC_2[i]];
			compressKey[i] = realKey[ PC_2[i]-1];
		subKey[round] = compressKey;
	}
	//打印
	for (int i = 0; i < 16; i++)
	{
		cout << "子密钥K" << i + 1 << "为:";
		for (int j = 0; j < 48; j++)
		{
			cout << subKey[i][j];
		}
		cout << endl;
	}
}

/**
 *  工具函数：将char字符数组转为二进制 ---------------64位转换
 */
bitset<64> charToBitset(string hexDigit)
{
	bitset<64> bits;
	int f, c, i;
	char e;
	for (int f = 0, i = 0; f <= hexDigit.length(); f++)
	{
		e = hexDigit[f];
		if (e >= 'A' && e <= 'F')
		{
			int a = static_cast<int>(e - 'A' + 10);
			bitset<4> binary(a);
			bits[i + 0] = binary[3];
			bits[i + 1] = binary[2];
			bits[i + 2] = binary[1];
			bits[i + 3] = binary[0];
			i = i + 4;
		}
		else if (isdigit(e))
		{
			int b = static_cast<int>(e - '0');
			bitset<4> binary(b);
			bits[i + 0] = binary[3];
			bits[i + 1] = binary[2];
			bits[i + 2] = binary[1];
			bits[i + 3] = binary[0];
			i = i + 4;
		}
	}
	//cout << "二进制表示为：";
	//for (int i = 0; i < 64; i++)
	//{
	//	cout << bits[i];             程序运行时间限制，
	//}
	//cout << endl;
	return bits;
}

/**
*   工具函数：将二进制bitset转换为字符数组---------------64位转换
*/
string bitToChar(bitset<64> cipher) {
	string cipher_result=""; //保存加密后的十六进制
	for (int i = 0; i <= 60; i=i+4) {
		int k;
		k = cipher[i] * 8 + cipher[i + 1] * 4 + cipher[i + 2] * 2 + cipher[i + 3] * 1;
		switch (k)
		{
			case 0:cipher_result.push_back('0'); break;
			case 1:cipher_result.push_back('1'); break;
			case 2:cipher_result.push_back('2'); break;
			case 3:cipher_result.push_back('3'); break;
			case 4:cipher_result.push_back('4'); break;
			case 5:cipher_result.push_back('5'); break;
			case 6:cipher_result.push_back('6'); break;
			case 7:cipher_result.push_back('7'); break;
			case 8:cipher_result.push_back('8'); break;
			case 9:cipher_result.push_back('9'); break;
			case 10:cipher_result.push_back('A'); break;
			case 11:cipher_result.push_back('B'); break;
			case 12:cipher_result.push_back('C'); break;
			case 13:cipher_result.push_back('D'); break;
			case 14:cipher_result.push_back('E'); break;
			case 15:cipher_result.push_back('F'); break;
			default:
				break;
		}
	}
	return cipher_result;
}


/**
 *  工具函数：将char字符数组转为二进制 ---------------8位转换
 */
bitset<8> charToBitset_8(string hexDigit)
{
	bitset<8> bits;
	int f, c, i;
	char e;
	for (int f = 0, i = 0; f <= hexDigit.length(); f++)
	{
		e = hexDigit[f];
		if (e >= 'A' && e <= 'F')
		{
			int a = static_cast<int>(e - 'A' + 10);
			bitset<4> binary(a);
			bits[i + 0] = binary[3];
			bits[i + 1] = binary[2];
			bits[i + 2] = binary[1];
			bits[i + 3] = binary[0];
			i = i + 4;
		}
		else if (isdigit(e))
		{
			int b = static_cast<int>(e - '0');
			bitset<4> binary(b);
			bits[i + 0] = binary[3];
			bits[i + 1] = binary[2];
			bits[i + 2] = binary[1];
			bits[i + 3] = binary[0];
			i = i + 4;
		}
	}
	//cout << "二进制表示为：";
	//for (int i = 0; i < 8; i++)
	//{
	//	cout << bits[i];
	//}
	//cout << endl;
	return bits;
}

/**
*   工具函数：将二进制bitset转换为字符数组---------------8位转换
*/
string bitToChar_8(bitset<8> cipher) {
	string cipher_result = ""; //保存加密后的十六进制
	for (int i = 0; i <= 4; i = i + 4) {
		int k;
		k = cipher[i] * 8 + cipher[i + 1] * 4 + cipher[i + 2] * 2 + cipher[i + 3] * 1;
		switch (k)
		{
		case 0:cipher_result.push_back('0'); break;
		case 1:cipher_result.push_back('1'); break;
		case 2:cipher_result.push_back('2'); break;
		case 3:cipher_result.push_back('3'); break;
		case 4:cipher_result.push_back('4'); break;
		case 5:cipher_result.push_back('5'); break;
		case 6:cipher_result.push_back('6'); break;
		case 7:cipher_result.push_back('7'); break;
		case 8:cipher_result.push_back('8'); break;
		case 9:cipher_result.push_back('9'); break;
		case 10:cipher_result.push_back('A'); break;
		case 11:cipher_result.push_back('B'); break;
		case 12:cipher_result.push_back('C'); break;
		case 13:cipher_result.push_back('D'); break;
		case 14:cipher_result.push_back('E'); break;
		case 15:cipher_result.push_back('F'); break;
		default:
			break;
		}
	}
	return cipher_result;
}

/**
 *  DES加密
 */
bitset<64> encrypt(bitset<64>& plain)
{
	bitset<64> cipher;
	bitset<64> currentBits;
	bitset<32> left;
	bitset<32> right;
	bitset<32> newLeft;
	// 第一步：初始置换IP
	for (int i = 0; i < 64; ++i)
		/*currentBits[63 - i] = plain[64 - IP[i]];*/
		currentBits[i] = plain[ IP[i]-1];
	
	cout << "初始置换后的明文：";    //本来是用来调bug 的，但是程序加密时间太慢了，必须优化
	for (int i = 0; i < 64; i++)
	{
		cout << currentBits[i];
	}
	cout << endl;

	// 第二步：获取 Li 和 Ri
	for (int i = 32; i < 64; ++i)
		/*left[i - 32] = currentBits[i];*/
		right[i - 32] = currentBits[i];
	for (int i = 0; i < 32; ++i)
		/*right[i] = currentBits[i];*/
		left[i] = currentBits[i];
	// 第三步：共16轮迭代
	for (int round = 0; round < 16; ++round)
	{
		newLeft = right;
		right = left ^ f(right, subKey[round]);
		left = newLeft;
	}
	// 第四步：合并L16和R16，注意合并为 R16L16
	for (int i = 0; i < 32; ++i)
		/*cipher[i] = left[i];*/
		cipher[i] = right[i];
	for (int i = 32; i < 64; ++i)
		/*cipher[i] = right[i - 32];*/
		cipher[i] = left[i - 32];
	// 第五步：结尾置换IP-1
	currentBits = cipher;
	for (int i = 0; i < 64; ++i)
		//cipher[63 - i] = currentBits[64 - IP_1[i]];
		cipher[ i] = currentBits[ IP_1[i]-1];
	// 返回密文
	//cout << "密文二进制表示为：";
	//for (int i = 0; i < 64; i++)
	//{
	//	cout << cipher[i];
	//}
	/*cout << endl;*/
	return cipher;
}

/**
 *  DES解密
 */
bitset<64> decrypt(bitset<64>& cipher)
{
	bitset<64> plain;
	bitset<64> currentBits;
	bitset<32> left;
	bitset<32> right;
	bitset<32> newLeft;
	// 第一步：初始置换IP
	for (int i = 0; i < 64; ++i)
		/*currentBits[63 - i] = cipher[64 - IP[i]];*/
		currentBits[i] = cipher[ IP[i]-1];
	// 第二步：获取 Li 和 Ri
	for (int i = 32; i < 64; ++i)
		/*left[i - 32] = currentBits[i];*/
		right[i - 32] = currentBits[i];
	for (int i = 0; i < 32; ++i)
		/*right[i] = currentBits[i];*/
		left[i] = currentBits[i];
	// 第三步：共16轮迭代（子密钥逆序应用）
	for (int round = 0; round < 16; ++round)
	{
		newLeft = right;
		right = left ^ f(right, subKey[15 - round]);
		left = newLeft;
	}
	// 第四步：合并L16和R16，注意合并为 R16L16
	for (int i = 0; i < 32; ++i)
		/*plain[i] = left[i];*/
		plain[i] = right[i];
	for (int i = 32; i < 64; ++i)
		/*plain[i] = right[i - 32];*/
		plain[i] = left[i - 32];
	// 第五步：结尾置换IP-1
	currentBits = plain;
	for (int i = 0; i < 64; ++i)
		/*plain[63 - i] = currentBits[64 - IP_1[i]];*/
		plain[i] = currentBits[IP_1[i]-1];
	// 返回明文
	//cout << "解密的明文二进制表示为：";
	//for (int i = 0; i < 64; i++)
	//{
	//	cout << plain[i];
	//}
	//cout << endl;
	return plain;

}

/**
 *  CBC模式下初始异或过程，后续的模式也会使用
 */
bitset<64> CBC_first(bitset<64> plain, bitset<64> vector){
	bitset<64> output;
	output = plain ^ vector;
	return output;
}

/**
 *  OFB模式寄存器---更新过程
 */
bitset<64> OFB_blocks(bitset<64> blocks_first , bitset<8>cipher) {
	bitset<8> offset=cipher;
	bitset<64> blocks = blocks_first;
	//将密文最左的八位加入到寄存器blocks中
	for (int j = 0; j < 56; j++)
	{
		blocks[j] = blocks[j + 8];
	}
	for (int j = 0; j < 8; j++)
	{
		blocks[j + 56] = offset[j];
	}
	return blocks;
}

/**
 *  OFB模式寄存器---初始化过程
 */
bitset<64> OFB_blocks_first(bitset<64> blocks_first) {
	bitset<64> blocks = blocks_first;
	return blocks;
}

/**
 *  OFB模式，八位异或
 */
bitset<8> OFB_xor(bitset<8> plain, bitset<64> vector) {
	bitset<8> output;
	for (int i = 0; i < 8; i++)
	{
		output[i] = plain[i] ^ vector[i];
	}
	return output;
}

/**
 *  OFB模式寄存器---更新过程
 */
bitset<64> OFB_blocks(bitset<64> blocks_first, bitset<64>cipher) {
	bitset<8> offset;
	//取最左变的八位
	for (int i = 0; i < 8; i++)
	{
		offset[i] = cipher[i];
	}
	bitset<64> blocks = blocks_first;
	//将密文最左的八位加入到寄存器blocks中
	for (int j = 0; j < 56; j++)
	{
		blocks[j] = blocks[j + 8];
	}
	for (int j = 0; j < 8; j++)
	{
		blocks[j + 56] = offset[j];
	}
	return blocks;
}



void ECB_encrypt(string plainfile, string keyfile, string cipherfile) {
	string plain_whole;//一行明文数据
	string cipher_file;//密文文件
	fstream file1; //随时保存文件
	fstream file2; //打开明文文件
	//本来我有个密钥打开的，不过放到这里的话，运行时间太慢了，于是就放到主函数中了
	file2.open(plainfile, ios::in);

	if (!file2.is_open())
	{
		cout << "未成功打开明文文件" << endl;
	}


	while (getline(file2,plain_whole))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = plain_whole.length() / 16;
		for (int i = 0,k=0; i < num_block; i++,k+=16)
		{
			//加密过程
			string ss = plain_whole.substr(k, 16);//每次读取16个字节的数据
			string cipher_Hex;
			bitset<64> plain = charToBitset(ss.c_str());// 二进制的明文和密钥
			bitset<64> cipher = encrypt(plain);
			//密文十六进制写入
			cipher_Hex = bitToChar(cipher);
			file1.open(cipherfile, ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	file2.close();

	//解密过程
	file2.open(cipherfile, ios::in);
	while (getline(file2, cipher_file))
	{
		int num_block;
		num_block = cipher_file.length() / 16;
		for (int i = 0, k = 0; i < num_block; i++, k += 16)
		{
			//加密过程
			string ss = cipher_file.substr(k, 16);//每次读取16个字节的数据
			string plain_Hex;
			bitset<64> cipher = charToBitset(ss.c_str());// 转换为二进制
			//key = charToBitset(key_file.c_str());
			////生成16个子密钥
			//generateKeys();
			bitset<64> plain = decrypt(cipher);
			//明文十六进制写入
			plain_Hex = bitToChar(plain);
			file1.open("D://ECB_test_plain.txt", ios::app);
			file1 << plain_Hex;
			file1.close();
		}
	}
	file2.close();
}
void CBC_encrypt(string plainfile, string keyfile, string ivfile,string cipherfile) {
	string plain_whole;//一行明文数据
	string key_file; //密钥的十六进制
	string iv_file; //初始化向量
	string cipher_file;//密文文件
	fstream file1; //随时保存文件
	fstream file2; //打开明文文件
	fstream file3; //打开密钥文件、初始的向量文件
	file2.open(plainfile, ios::in);
	file3.open(keyfile, ios::in);
	if (!file2.is_open())
	{
		cout << "未成功打开明文文件" << endl;
	}
	if (!file3.is_open())
	{
		cout << "未成功打开密钥文件" << endl;
	}
	getline(file3, key_file);
	file3.close();
	file3.open(ivfile, ios::in);
	if (!file3.is_open())
	{
		cout << "未成功打开初始向量文件" << endl;
	}
	getline(file3, iv_file);

	bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	key = charToBitset(key_file.c_str());
	//生成16个子密钥
	generateKeys();
	
	//加密过程
	while (getline(file2, plain_whole))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = plain_whole.length() / 16;
		for (int i = 0, k = 0; i < num_block; i++, k += 16)
		{
			//加密过程
			string ss = plain_whole.substr(k, 16);//每次读取16个字节的数据
			string cipher_Hex;//密文的十六进制
			bitset<64> plain = charToBitset(ss.c_str());// 二进制的明文和密钥
			//key = charToBitset(key_file.c_str());
			////生成16个子密钥
			//generateKeys();

			bitset<64>CBC_plain = CBC_first(plain, iv_Hex);
			bitset<64> cipher = encrypt(CBC_plain);
			iv_Hex = cipher;//将上一次加密的密文，与下一次的初始明文块异或
			
			//密文十六进制写入
			cipher_Hex = bitToChar(cipher);
			file1.open(cipherfile, ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	file2.close();
	
	//解密过程
	file2.open(cipherfile, ios::in);
	iv_Hex = charToBitset(iv_file.c_str());//重新初始化初始向量
	
	while (getline(file2, cipher_file))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = cipher_file.length() / 16;
		for (int i = 0, k = 0; i < num_block; i++, k += 16)
		{
			//加密过程  
			string ss = cipher_file.substr(k, 16);//每次读取16个字节的数据
			string plain_Hex;//明文的十六进制
			bitset<64> cipher = charToBitset(ss.c_str());// 二进制的密文和密钥
			//key = charToBitset(key_file.c_str());
			////生成16个子密钥
			//generateKeys();

			bitset<64> after_decrypt = decrypt(cipher);//解密密文
			bitset<64>CBC_plain = CBC_first(after_decrypt, iv_Hex);//解密之后的与初始向量异或,得到真正的明文
			iv_Hex = cipher;//将密文与下一次解密后的密文进行异或得到明文

			//明文十六进制写入
			plain_Hex = bitToChar(CBC_plain);
			file1.open("D://CBC_test_plain.txt", ios::app);
			file1 << plain_Hex;
			file1.close();
		}
	}
	file2.close();
}
void CFB_encrypt(string plainfile, string keyfile, string ivfile, string cipherfile){
	string plain_whole;//一行明文数据
	string key_file; //密钥的十六进制
	string iv_file; //初始向量文件
	string cipher_file; //密文文件
	fstream file1; //随时保存文件
	fstream file2; //打开明文文件
	fstream file3; //打开密钥文件、初始的向量文件
	file2.open(plainfile, ios::in);
	file3.open(keyfile, ios::in);
	if (!file2.is_open())
	{
		cout << "未成功打开明文文件" << endl;
	}
	if (!file3.is_open())
	{
		cout << "未成功打开密钥文件" << endl;
	}
	getline(file3, key_file);
	file3.close();

	file3.open(ivfile, ios::in);
	if (!file3.is_open())
	{
		cout << "未成功打开初始向量文件" << endl;
	}
	getline(file3, iv_file);

	bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	bitset<64> blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	key = charToBitset(key_file.c_str());//二进制的密钥
	//生成16个子密钥
	generateKeys();
	//加密过程
	while (getline(file2, plain_whole))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = plain_whole.length() / 2;
		for (int i = 0, k = 0; i < num_block; i++, k += 2)
		{
			//加密过程
			string ss = plain_whole.substr(k, 2);//每次读取8bits的数据
			string cipher_Hex;//密文的十六进制
			bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文

			
			bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
			bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
			blocks = OFB_blocks(blocks, cipher);//更新寄存器
			
			//密文十六进制写入
			cipher_Hex = bitToChar_8(cipher);
			file1.open(cipherfile, ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	file2.close();
	
	//解密过程
	blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	file2.open(cipherfile, ios::in);
	while (getline(file2, cipher_file))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = cipher_file.length() / 2;
		for (int i = 0, k = 0; i < num_block; i++, k += 2)
		{
			//加密过程
			string ss = cipher_file.substr(k, 2);//每次读取8bits的数据
			string cipher_Hex;//密文的十六进制
			bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
			//key = charToBitset(key_file.c_str());//二进制的密钥
			////生成16个子密钥
			//generateKeys();

			bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
			bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
			blocks = OFB_blocks(blocks, plain);//更新寄存器

			//密文十六进制写入
			cipher_Hex = bitToChar_8(cipher);
			file1.open("D://CFB_plain.txt", ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	file2.close();
}
void OFB_encrypt(string plainfile, string keyfile, string ivfile, string cipherfile) {
	string plain_whole;//一行明文数据
	string key_file; //密钥的十六进制
	string iv_file;  //向量
	string cipher_file;//密文文件
	fstream file1; //随时保存文件
	fstream file2; //打开明文文件
	fstream file3; //打开密钥文件、初始的向量文件
	file2.open(plainfile, ios::in);
	file3.open(keyfile, ios::in);
	if (!file2.is_open())
	{
		cout << "未成功打开明文文件" << endl;
	}
	if (!file3.is_open())
	{
		cout << "未成功打开密钥文件" << endl;
	}
	getline(file3, key_file);
	file3.close();

	file3.open(ivfile, ios::in);
	if (!file3.is_open())
	{
		cout << "未成功打开初始向量文件" << endl;
	}
	getline(file3, iv_file);

	bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	bitset<64> blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	
	key = charToBitset(key_file.c_str());//二进制的密钥
	//生成16个子密钥
	generateKeys();
	//加密
	while (getline(file2, plain_whole))
	{
		cout << "读取plain成功" << endl;
		int num_block;
		num_block = plain_whole.length() / 2;
		for (int i = 0, k = 0; i < num_block; i++, k += 2)
		{
			//加密过程
			string ss = plain_whole.substr(k, 2);//每次读取8bits的数据
			string cipher_Hex;//密文的十六进制
			bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
			//key = charToBitset(key_file.c_str());//二进制的密钥
			////生成16个子密钥
			//generateKeys();

			bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
			bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
			blocks = OFB_blocks(blocks, blocks_encrypt);//更新寄存器

			//密文十六进制写入
			cipher_Hex = bitToChar_8(cipher);
			file1.open(cipherfile, ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	file2.close();
	
	//解密
	blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	file2.open(cipherfile, ios::in);
	while (getline(file2,cipher_file))
	{
		cout << "读取cipher成功" << endl;
		int num_block;
		num_block = cipher_file.length() / 2;
		for (int i = 0, k = 0; i < num_block; i++, k += 2)
		{
			string ss = cipher_file.substr(k, 2);//每次读取8bits的数据
			string cipher_Hex;//密文的十六进制
			bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
			//key = charToBitset(key_file.c_str());//二进制的密钥
			////生成16个子密钥
			//generateKeys();

			bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
			bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
			blocks = OFB_blocks(blocks, blocks_encrypt);//更新寄存器

			//密文十六进制写入
			cipher_Hex = bitToChar_8(cipher);
			file1.open("D://OFB_plain.txt", ios::app);
			file1 << cipher_Hex;
			file1.close();
		}
	}
	
	file2.close();

}
/**********************************************************************/
/* 主函数 包含了四种模式最初的测试代码   后来写到了分函数中 */
/*    1.提取命令，就是分割输入的命令                  */
/*    2.根据输入的命令，采取不同的模式             */
/**********************************************************************/

int main() {

	enum encrypt_mode { ECB, CBC, CFB, OFB }encrypt_mode; // 定义枚举类型encrypt_mode

	string s;    //待分割的字符串，含有很多空格 
	string plainfile; //指定明文文件的位置和名称
	string keyfile; //指定密钥文件的位置和名称
	string vifile; //指定初始化向量文件的位置和名称
	string mode; //指定加密的操作模式
	string cipherfile;// 指定密文文件的位置和名称。

	string plain;  //明文
	vector<string> args;  //用于存放分割后的字符串
	getline(cin, s);//输入一行字符串  
	stringstream line(s);//创建字符流对象  
	while (getline(line, s, ' '))//按空格分隔字符串  
	{
		args.push_back(s);
	}
	//分割字符，从用户输入中读取相应的参数
	vector<string>::iterator it;
	for (it = args.begin(); it != args.end(); it++) {
		if (*it == "-p") {
			string* t;
			plainfile = *(++it);
			cout << "指定明文文件的位置和名称" << plainfile << endl;
		}
		else if (*it == "-k") {
			string* t;
			keyfile = *(++it);
			cout << "指定密钥文件的位置和名称" << keyfile << endl;
		}
		else if (*it == "[-v") {
			string* t;
			vifile = *(++it);
			vifile.pop_back();
			cout << "指定初始化向量文件的位置和名称" << vifile << endl;
		}
		else if (*it == "-m") {
			string* t;
			mode = *(++it);
			cout << "指定加密的操作模式" << mode << endl;
			if (mode == "ECB") encrypt_mode = ECB;
			else if (mode == "CBC") encrypt_mode = CBC;
			else if (mode == "CFB") encrypt_mode = CFB;
			else if (mode == "OFB") encrypt_mode = OFB;
		}
		else if (*it == "-c") {
			string* t;
			cipherfile = *(++it);
			cout << "指定密文文件的位置和名称" << cipherfile << endl;
		}
	}
	switch (encrypt_mode)
	{
	case ECB: {
		clock_t startTime, endTime;
		startTime = clock();//计时开始
		fstream file3; //打开密钥文件
		string key_file;//密钥文件
		file3.open(keyfile, ios::in);
		if (!file3.is_open())
		{
			cout << "未成功打开密钥文件" << endl;
		}
		getline(file3, key_file);
		//加密过程
		key = charToBitset(key_file.c_str());
		//生成16个子密钥
		generateKeys();
		file3.close();
		for (int i = 0; i < 1; i++)
		{
			ECB_encrypt(plainfile, keyfile, cipherfile);
		}
		endTime = clock();//计时结束
		cout << "The run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
		break;
	}
	case CBC: {
		clock_t startTime, endTime;
		startTime = clock();//计时开始
		CBC_encrypt(plainfile, keyfile, vifile, cipherfile);
		endTime = clock();//计时结束
		cout << "The CBC run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
		break;
	}
	case CFB: {
		clock_t startTime, endTime;
		startTime = clock();//计时开始
		for (int i = 0; i < 1; i++)
			CFB_encrypt(plainfile, keyfile, vifile, cipherfile);
		endTime = clock();//计时结束
		cout << "The CFB run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
		break;
	}
	case OFB: {
		clock_t startTime, endTime;
		startTime = clock();//计时开始
		for (int i = 0; i < 1; i++)
			OFB_encrypt(plainfile, keyfile, vifile, cipherfile);
		endTime = clock();//计时结束
		cout << "The OFB run time is: " << (double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
		break;
		break;
	}
	default: cout << "尚无此种加密方式" << endl; break;
	}
	return 0;
		/**
		*  ECB模式
		*/
	//string plain_whole;//一行明文数据
	//string key_file;//密钥文件
	//string cipher_file;//密文文件
	//fstream file1; //随时保存文件
	//fstream file2; //打开明文文件
	//fstream file3; //打开密钥文件
	//file2.open("D://des_plain.txt", ios::in);
	//file3.open("D://des_key.txt", ios::in);
	//if (!file2.is_open())
	//{
	//	cout << "未成功打开明文文件" << endl;
	//}
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开密钥文件" << endl;
	//}
	//getline(file3, key_file);
	//file3.close();

	////加密过程
	//while (getline(file2,plain_whole))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = plain_whole.length() / 16;
	//	for (int i = 0,k=0; i < num_block; i++,k+=16)
	//	{
	//		//加密过程
	//		string ss = plain_whole.substr(k, 16);//每次读取16个字节的数据
	//		string cipher_Hex;
	//		bitset<64> plain = charToBitset(ss.c_str());// 二进制的明文和密钥
	//		key = charToBitset(key_file.c_str());
	//		//生成16个子密钥
	//		generateKeys();
	//		bitset<64> cipher = encrypt(plain);
	//		//密文十六进制写入
	//		cipher_Hex = bitToChar(cipher);
	//		file1.open("D://ECB_encrypt.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();

	////解密过程
	//file2.open("D://ECB_encrypt.txt", ios::in);
	//while (getline(file2, cipher_file))
	//{
	//	int num_block;
	//	num_block = cipher_file.length() / 16;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 16)
	//	{
	//		//加密过程
	//		string ss = cipher_file.substr(k, 16);//每次读取16个字节的数据
	//		string plain_Hex;
	//		bitset<64> cipher = charToBitset(ss.c_str());// 转换为二进制
	//		key = charToBitset(key_file.c_str());
	//		//生成16个子密钥
	//		generateKeys();
	//		bitset<64> plain = decrypt(cipher);
	//		//明文十六进制写入
	//		plain_Hex = bitToChar(plain);
	//		file1.open("D://ECB_plain.txt", ios::app);
	//		file1 << plain_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();


	/**
	*  CBC模式
	*/
	//string plain_whole;//一行明文数据
	//string key_file; //密钥的十六进制
	//string iv_file; //初始化向量
	//string cipher_file;//密文文件
	//fstream file1; //随时保存文件
	//fstream file2; //打开明文文件
	//fstream file3; //打开密钥文件、初始的向量文件
	//file2.open("D://des_plain.txt", ios::in);
	//file3.open("D://des_key.txt", ios::in);
	//if (!file2.is_open())
	//{
	//	cout << "未成功打开明文文件" << endl;
	//}
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开密钥文件" << endl;
	//}
	//getline(file3, key_file);
	//file3.close();
	//file3.open("D://des_iv.txt", ios::in);
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开初始向量文件" << endl;
	//}
	//getline(file3, iv_file);

	//bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	//
	////加密过程
	//while (getline(file2, plain_whole))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = plain_whole.length() / 16;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 16)
	//	{
	//		//加密过程
	//		string ss = plain_whole.substr(k, 16);//每次读取16个字节的数据
	//		string cipher_Hex;//密文的十六进制
	//		bitset<64> plain = charToBitset(ss.c_str());// 二进制的明文和密钥
	//		key = charToBitset(key_file.c_str());
	//		//生成16个子密钥
	//		generateKeys();

	//		bitset<64>CBC_plain = CBC_first(plain, iv_Hex);
	//		bitset<64> cipher = encrypt(CBC_plain);
	//		iv_Hex = cipher;//将上一次加密的密文，与下一次的初始明文块异或
	//		
	//		//密文十六进制写入
	//		cipher_Hex = bitToChar(cipher);
	//		file1.open("D://CBC_encrypt.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();
	//
	////解密过程
	//file2.open("D://CBC_encrypt.txt", ios::in);
	//iv_Hex = charToBitset(iv_file.c_str());//重新初始化初始向量
	//
	//while (getline(file2, cipher_file))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = cipher_file.length() / 16;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 16)
	//	{
	//		//加密过程  
	//		string ss = cipher_file.substr(k, 16);//每次读取16个字节的数据
	//		string plain_Hex;//明文的十六进制
	//		bitset<64> cipher = charToBitset(ss.c_str());// 二进制的密文和密钥
	//		key = charToBitset(key_file.c_str());
	//		//生成16个子密钥
	//		generateKeys();

	//		bitset<64> after_decrypt = decrypt(cipher);//解密密文
	//		bitset<64>CBC_plain = CBC_first(after_decrypt, iv_Hex);//解密之后的与初始向量异或,得到真正的明文
	//		iv_Hex = cipher;//将密文与下一次解密后的密文进行异或得到明文

	//		//明文十六进制写入
	//		plain_Hex = bitToChar(CBC_plain);
	//		file1.open("D://CBC_plain.txt", ios::app);
	//		file1 << plain_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();

	/**
	*  CFB模式
	*/
	//string plain_whole;//一行明文数据
	//string key_file; //密钥的十六进制
	//string iv_file; //初始向量文件
	//string cipher_file; //密文文件
	//fstream file1; //随时保存文件
	//fstream file2; //打开明文文件
	//fstream file3; //打开密钥文件、初始的向量文件
	//file2.open("D://des_plain.txt", ios::in);
	//file3.open("D://des_key.txt", ios::in);
	//if (!file2.is_open())
	//{
	//	cout << "未成功打开明文文件" << endl;
	//}
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开密钥文件" << endl;
	//}
	//getline(file3, key_file);
	//file3.close();

	//file3.open("D://des_iv.txt", ios::in);
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开初始向量文件" << endl;
	//}
	//getline(file3, iv_file);

	//bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	//bitset<64> blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	//
	////加密过程
	//while (getline(file2, plain_whole))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = plain_whole.length() / 2;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 2)
	//	{
	//		//加密过程
	//		string ss = plain_whole.substr(k, 2);//每次读取8bits的数据
	//		string cipher_Hex;//密文的十六进制
	//		bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
	//		key = charToBitset(key_file.c_str());//二进制的密钥
	//		//生成16个子密钥
	//		generateKeys();
	//		
	//		bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
	//		bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
	//		blocks = OFB_blocks(blocks, cipher);//更新寄存器
	//		
	//		//密文十六进制写入
	//		cipher_Hex = bitToChar_8(cipher);
	//		file1.open("D://CFB_encrypt.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();
	//
	////解密过程
	//blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	//file2.open("D://CFB_encrypt.txt", ios::in);
	//while (getline(file2, cipher_file))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = cipher_file.length() / 2;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 2)
	//	{
	//		//加密过程
	//		string ss = cipher_file.substr(k, 2);//每次读取8bits的数据
	//		string cipher_Hex;//密文的十六进制
	//		bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
	//		key = charToBitset(key_file.c_str());//二进制的密钥
	//		//生成16个子密钥
	//		generateKeys();

	//		bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
	//		bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
	//		blocks = OFB_blocks(blocks, plain);//更新寄存器

	//		//密文十六进制写入
	//		cipher_Hex = bitToChar_8(cipher);
	//		file1.open("D://CFB_plain.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();


	/**
	*  OFB模式
	*/
	//string plain_whole;//一行明文数据
	//string key_file; //密钥的十六进制
	//string iv_file;  //向量
	//string cipher_file;//密文文件
	//fstream file1; //随时保存文件
	//fstream file2; //打开明文文件
	//fstream file3; //打开密钥文件、初始的向量文件
	//file2.open("D://des_plain.txt", ios::in);
	//file3.open("D://des_key.txt", ios::in);
	//if (!file2.is_open())
	//{
	//	cout << "未成功打开明文文件" << endl;
	//}
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开密钥文件" << endl;
	//}
	//getline(file3, key_file);
	//file3.close();

	//file3.open("D://des_iv.txt", ios::in);
	//if (!file3.is_open())
	//{
	//	cout << "未成功打开初始向量文件" << endl;
	//}
	//getline(file3, iv_file);

	//bitset<64> iv_Hex = charToBitset(iv_file.c_str());
	//bitset<64> blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	//
	////加密
	//while (getline(file2, plain_whole))
	//{
	//	cout << "读取plain成功" << endl;
	//	int num_block;
	//	num_block = plain_whole.length() / 2;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 2)
	//	{
	//		//加密过程
	//		string ss = plain_whole.substr(k, 2);//每次读取8bits的数据
	//		string cipher_Hex;//密文的十六进制
	//		bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
	//		key = charToBitset(key_file.c_str());//二进制的密钥
	//		//生成16个子密钥
	//		generateKeys();

	//		bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
	//		bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
	//		blocks = OFB_blocks(blocks, blocks_encrypt);//更新寄存器

	//		//密文十六进制写入
	//		cipher_Hex = bitToChar_8(cipher);
	//		file1.open("D://OFB_encrypt.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//file2.close();
	//
	////解密
	//blocks = OFB_blocks_first(iv_Hex);//用初始向量初始化寄存器
	//file2.open("D://OFB_encrypt.txt", ios::in);
	//while (getline(file2,cipher_file))
	//{
	//	cout << "读取cipher成功" << endl;
	//	int num_block;
	//	num_block = cipher_file.length() / 2;
	//	for (int i = 0, k = 0; i < num_block; i++, k += 2)
	//	{
	//		string ss = cipher_file.substr(k, 2);//每次读取8bits的数据
	//		string cipher_Hex;//密文的十六进制
	//		bitset<8> plain = charToBitset_8(ss.c_str());// 二进制的明文
	//		key = charToBitset(key_file.c_str());//二进制的密钥
	//		//生成16个子密钥
	//		generateKeys();

	//		bitset<64> blocks_encrypt = encrypt(blocks); //反馈过程中，对寄存器中的64位数据进行加密
	//		bitset<8> cipher = OFB_xor(plain, blocks_encrypt); //与明文块的异或
	//		blocks = OFB_blocks(blocks, blocks_encrypt);//更新寄存器

	//		//密文十六进制写入
	//		cipher_Hex = bitToChar_8(cipher);
	//		file1.open("D://OFB_plain.txt", ios::app);
	//		file1 << cipher_Hex;
	//		file1.close();
	//	}
	//}
	//
	//file2.close();

}